﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using MyRoguelike.Tools;
using MyRoguelike.Game.Items;

namespace MyRoguelike.Game.Objects
{
    class GOCreature : GameObject
    {
        private List<Point> _walkPath = new List<Point>();
        private Inventory _inventory;

        private int _health;

        public int Damage { get; set; }
        public int Health
        {
            get { return _health; }
            set
            {
                _health = value;
                if (_health > MaxHealth) _health = MaxHealth;
            }
        }
        public int MaxHealth { get; set; }
        public int Defense { get; set; }
        public float AggrRange { get; set; }

        // Is die?
        public bool IsDeath
        {
            get
            {
                if (Health <= 0) return true;
                return false;
            }
        }

        // Creature inventory
        public Inventory Inventory
        {
            get { return _inventory; }
        }

        public List<Point> WalkPath
        {
            get { return _walkPath; }
        }

        public GOCreature(Level level) 
            : base(level)
        {
            _inventory = new Inventory(this);

            IsCreature = true;
        }

        /// <summary>
        /// Create the walk path to coordinate
        /// </summary>
        /// <param name="coord">Coordinate</param>
        public void Go(Point coord)
        {
            _walkPath.Clear();

            int[,] m = Pathfinding.ConvertMap(_level.Map, _level.MapSize);

            List<Point> path = Pathfinding.Find(m, _level.MapSize, 
                _level.GetObjectCoord(this), coord);

            if (path != null)
            {
                _walkPath = path;
                _walkPath.RemoveAt(0);
            }
        }

        /// <summary>
        /// Walk step
        /// </summary>
        public void Walk()
        {
            if (_walkPath.Count > 0)
            {
                if (_level.MoveTo(this, _walkPath[0]))
                    _walkPath.RemoveAt(0);
            }
        }

        /// <summary>
        /// Attacking game object
        /// </summary>
        /// <param name="gameObject">Game object</param>
        public void Attack(GameObject gameObject)
        {
            if (gameObject.IsCreature)
            {
                GOCreature creature = (GOCreature) gameObject;

                // Calc damage
                int d = Damage - creature.Defense;

                if (d > 0)
                    creature.Health = creature.Health - d;
                else creature.Health = creature.Health - 1;
            }

            if (gameObject.IsPlayer)
            {
                GOPlayer player = (GOPlayer)gameObject;

                // Calc damage
                int d = Damage - player.Defense;

                if (d > 0)
                    player.Health = player.Health - d;
                else player.Health = player.Health - 1;
            }
        }
    }
}
